package com.bytedance.ttnet;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.bytedance.common.utility.Logger;
import com.bytedance.frameworks.baselib.network.http.a.SSCookieHandler;
import com.bytedance.frameworks.baselib.network.http.AppConfig;
import com.bytedance.frameworks.baselib.network.http.AppConfig.a;
import com.bytedance.frameworks.baselib.network.http.AppConfig.e;
import com.bytedance.frameworks.baselib.network.http.util.ProcessUtils;
import com.bytedance.ttnet.b.HttpRequestInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.CookieHandler;

public final class cc {
    private static bb sITTNetDepend;
    private static long b = 0;

    public static bb a() {
        if (cc.sITTNetDepend == null) {
            throw new IllegalArgumentException("sITTNetDepend is null");
        }

        return cc.sITTNetDepend;
    }

    public static void tryInitTTNet(Context arg2, a arg3, e arg4, AppConfig.d$c arg5) {
        if (arg2 == null) {
            throw new IllegalArgumentException("tryInitTTNet context is null");
        }

        AppConfig.a(arg3);
        HttpRequestInfo.injectCreate();
        if (ProcessUtils.a(arg2)) {
            cc.b(arg2, true);
            com.bytedance.ttnet.a.AppConfig.getInstance(arg2).a(false);
        }

        if (ProcessUtils.b(arg2)) {
            new NetworkAsyncInitThread("NetWork-AsyncInit", arg2).start();
            AppConfig.a(arg5);
            com.bytedance.ttnet.a.AppConfig.getInstance(arg2);
            AppConfig.a(arg4);
        }
    }

    static void a(Context arg0, boolean arg1) {
        cc.b(arg0, arg1);
    }

    public static void a(bb arg0) {
        cc.sITTNetDepend = arg0;
    }

    public static void a(String arg0) {
        AppConfig.a(arg0);
    }

    private static void b(Context arg6, boolean arg7) {
        CookieManager v1;
        try {
            CookieSyncManager.createInstance(arg6);
            v1 = CookieManager.getInstance();
            v1.setAcceptCookie(true);
            cc.b = System.currentTimeMillis();
            if (!arg7) {
                AppConfig.b(true);
            } else if (CookieHandler.getDefault() != null) {
//                goto label_9;
            } else if (cc.b <= 0) {
//                goto label_9;
            } else {
                if (System.currentTimeMillis() - cc.b >= 1500) {
                    AppConfig.b(true);
                } else {
                    AppConfig.b(false);
                }

                CookieHandler.setDefault(new SSCookieHandler(CookieManager.getInstance()));
//                goto label_9;
            }
            if (!Logger.debug()) {
                return;
            }

            Logger.d("Process", " CookieManager = " + v1.toString() + " pid = " + String.valueOf(Process.myPid()));
        } catch (Throwable v0) {

            label_44:
            v0.printStackTrace();
            if (!ProcessUtils.b(arg6)) {
                return;
            }

            if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
                return;
            }

            new Handler(Looper.getMainLooper()).post(new com.bytedance.ttnet.e(arg6, arg7));
            JSONObject v1_1 = new JSONObject();
            try {
                v1_1.put("error", v0.getMessage());
            } catch (JSONException v0_1) {
                v0_1.printStackTrace();
            }

            if (cc.sITTNetDepend == null) {
                return;
            }
            cc.sITTNetDepend.a("async_init_cookie_manager_fail", v1_1);
        }
    }
}

